﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TradingServer.Core.Entities
{
	public enum CurrencyCode
	{
		RUR = 840,
		EUR = 978,
		USD = 810
	}

	public class Money : IComparable<Money>
	{
		public Money(decimal value, CurrencyCode currency)
		{
			Value = value;
			Currency = currency;
		}

		public decimal Value 
		{ 
			get { return (decimal) _value / 100; } 
			set { _value = (long) (value * 100); } 
		}
		public CurrencyCode Currency { get; set; }

		public static Money operator + (Money left, Money right)
		{
			if (left.Currency != right.Currency) throw new ArgumentException("Currencies of operands are not same");
			return new Money(left.Value + right.Value, left.Currency);
		}

		public static Money operator - (Money left, Money right)
		{
			if (left.Currency != right.Currency) throw new ArgumentException("Currencies of operands are not same");
			return new Money(left.Value - right.Value, left.Currency);
		}

		public static Money operator * (Money left, Money right)
		{
			if (left.Currency != right.Currency) throw new ArgumentException("Currencies of operands are not same");
			return new Money(left.Value * right.Value, left.Currency);
		}

		public static Money operator / (Money left, Money right)
		{
			if (left.Currency != right.Currency) throw new ArgumentException("Currencies of operands are not same");
			return new Money(left.Value / right.Value, left.Currency);
		}

		public static bool operator == (Money left, Money right)
		{
			return left.Equals(right);
		}

		public static bool operator != (Money left, Money right)
		{
			return !(left == right);
		}

		public override int GetHashCode()
		{
			return _value.GetHashCode() + Currency.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (obj == null) return false;
			if (!(obj is Money)) return false;
			Money m = obj as Money;
			return Value == m.Value && Currency == m.Currency;
		}

		#region IComparable<Money> Members

		public int CompareTo(Money other)
		{
			if (Equals(other)) return 0;
			if (Currency == other.Currency) return Value.CompareTo(other.Value);
			throw new InvalidOperationException("Cannot compare money with not same currency");
		}

		#endregion

		#region Private members

		private long _value;

		#endregion
	}
}
